//
//  CNStatus.swift
//  Ccweibo
//
//  Created by 陈锦聪 on 16/3/25.
//  Copyright © 2016年 Coco. All rights reserved.
//

import UIKit

class CNStatus: NSObject {

    /// 微博创建时间
    var created_at: String?
    /// 微博ID
    var id: Int64 = 0
    /// 微博信息内容
    var text: String? {
        didSet {
            attrText = text?.emoticonTextToEmoticonAttributedString(UIFont.systemFontOfSize(textFontLarge))
        }
    }
    /// 微博内容属性文本
    var attrText: NSAttributedString?
    /// 微博来源
    var source: String? {
        didSet {
            source = source?.sourceString
        }
    }
    /// 微博图片
    var pic_urls: [[String: AnyObject]]? {
        didSet {
            if pic_urls?.count == 0 {
                return
            }
            // 这里是需要初始化的!
            pictureURLs = [NSURL]()
            for dict in pic_urls! {
                guard let urlString = dict["thumbnail_pic"] as? String else {
                    continue
                }
                guard let url = NSURL(string: urlString) else {
                    continue
                }
                pictureURLs?.append(url)
            }
        }
    }
    /// 微博图片地址
    var pictureURLs: [NSURL]?
    /// 转发数
    var reposts_count: Int = 0
    /// 评论数
    var comments_count: Int = 0
    /// 表态数
    var attitudes_count: Int = 0
    /// 用户
    var user: CNUser?
    /// 转发微博
    var retweeted_status: CNStatus?
    /// 行高
    var rowHeight: CGFloat?
    
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    // 'setValuesForKeysWithDictionary'会调用'setValueForKey'进行赋值
    override func setValue(value: AnyObject?, forKey key: String) {
        if key == "user" {
            // '?'表示拆包后,有值执行if,为nil执行else
            if let dict = value as? [String: AnyObject] {
                user = CNUser(dict: dict)
            }
            return
        }
        if key == "retweeted_status" {
            if let dict = value as? [String: AnyObject] {
                retweeted_status = CNStatus(dict: dict)
            }
            return
        }
        super.setValue(value, forKey: key)
    }
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
//        CNPrint(item: "UndefinedKey:\(key)")
    }
    override var description: String {
        get {
            let key = ["created_at", "id", "text", "source", "pic_urls", "reposts_count", "comments_count", "attitudes_count"]
            return "\n\tStatus: \(dictionaryWithValuesForKeys(key).description)"
        }
    }
}
